package com.upcoming.deploy;

import com.alibaba.dubbo.common.utils.ClassHelper;
import com.upcoming.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

/**
 * 部署环境类，主要用户从环境变量和配置文件中加载服务配置信息
 */
public class DeployEnvironment implements Serializable {

    private static final long serialVersionUID = -2744685019563845823L;

    private static final Logger logger = LoggerFactory.getLogger(DeployEnvironment.class);

    /**
     * 部署目录
     */
    private static volatile String deployDir = null;

    private static volatile String confiDir = null;

    private static volatile String classDir = null;

    private static volatile String serviceName = null;

    private static volatile int servicePort = 0;

    private static volatile int servicePid = 0;

    private static volatile String serviceHost = null;

    private static volatile String dubboRegistry = null;

    private static volatile long startTime = 0L;

    private static volatile Properties properties;


    private DeployEnvironment() {

    }


    public static void initialize() {
        if (startTime == 0L) {
            startTime = System.currentTimeMillis();
        }
    }


    public static String getProperty(String key) {
        return getProperty(key, null);
    }

    public static String getProperty(String key, String defaultValue) {
        //从jvm变量中获取属性值
        String value = System.getProperty(key);
        if (value != null && value.length() > 0) {
            return value;
        }
        //从系统环境中获取变量值
        value = System.getenv(key);
        if (value != null && value.length() > 0) {
            return value;
        }
        //从配置文件中获取变量值
        Properties properties = getProperties();
        return properties.getProperty(key, defaultValue);
    }

    public static Properties getProperties() {
        if (properties == null) {
            synchronized (DeployEnvironment.class) {
                String path = System.getProperty(Constants.DEFAULT_PROPERTIES_KEY);
                if (path == null || path.length() == 0) {
                    path =Constants.DEFAULT_PROPERTIES;
                }
                properties = loadProperties(path);
            }
        }
        return properties;
    }

    public static Properties loadProperties(String fileName) {
        Properties properties = new Properties();
        //指定文件获取
        if (fileName.startsWith("/")) {
            try {
                FileInputStream input = new FileInputStream(fileName);
                try {
                    properties.load(input);
                } finally {
                    input.close();
                }
            } catch (Throwable t) {
                logger.warn("load file failed " + fileName + t.getMessage(), t);
            }
        }
        try {
            InputStream inputStream = ClassHelper.getClassLoader().getResourceAsStream(fileName);
            if (inputStream == null) {
                return null;
            }
            properties.load(inputStream);
        } catch (Throwable t) {
            logger.warn("load file failed " + fileName + t.getMessage(), t);
            return null;
        }
        return properties;
    }

    public static String getDeployDir() {
        return deployDir;
    }

    public static void setDeployDir(String deployDir) {
        DeployEnvironment.deployDir = deployDir;
    }

    public static String getConfiDir() {
        return confiDir;
    }

    public static void setConfiDir(String confiDir) {
        DeployEnvironment.confiDir = confiDir;
    }

    public static String getClassDir() {
        return classDir;
    }

    public static void setClassDir(String classDir) {
        DeployEnvironment.classDir = classDir;
    }

    public static String getServiceName() {
        return serviceName;
    }

    public static void setServiceName(String serviceName) {
        DeployEnvironment.serviceName = serviceName;
    }

    public static int getServicePort() {
        return servicePort;
    }

    public static void setServicePort(int servicePort) {
        DeployEnvironment.servicePort = servicePort;
    }

    public static int getServicePid() {
        return servicePid;
    }

    public static void setServicePid(int servicePid) {
        DeployEnvironment.servicePid = servicePid;
    }

    public static String getServiceHost() {
        return serviceHost;
    }

    public static void setServiceHost(String serviceHost) {
        DeployEnvironment.serviceHost = serviceHost;
    }

    public static String getDubboRegistry() {
        return dubboRegistry;
    }

    public static void setDubboRegistry(String dubboRegistry) {
        DeployEnvironment.dubboRegistry = dubboRegistry;
    }

    public static long getStartTime() {
        return startTime;
    }

    public static void setStartTime(long startTime) {
        DeployEnvironment.startTime = startTime;
    }
}
